package com.cskaoyan.market.service;

import com.cskaoyan.market.bo.WxCommentBo;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.vo.WxCommentVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class WxCommentServiceImpl implements WxCommentService{
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Override
    public Map<String, Object> count(Integer valueId, byte type) {
        Map<String ,Object> map=new HashMap<>();
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andTypeEqualTo(type);
        if (valueId!=null){
            criteria.andValueIdEqualTo(valueId);
        }
        long all = marketCommentMapper.countByExample(marketCommentExample);
        String[] strings = new String[0];
        criteria.andPicUrlsNotEqualTo(strings);
        long hasPic = marketCommentMapper.countByExample(marketCommentExample);
        map.put("hasPicCount",hasPic);
        map.put("allCount",all);
        return map;
    }

    @Override
    public List<WxCommentVo> list(Integer valueId, byte type, Integer limit, Integer page, Integer showType) {
        PageHelper.startPage(page,limit);
        List<WxCommentVo> voList=marketCommentMapper.selectWxCommentVos(valueId,type,showType);
        return voList;
    }

    @Override
    public MarketComment post(WxCommentBo wxCommentBo, MarketUser user) {
        MarketComment marketComment = new MarketComment();
        marketComment.setValueId(wxCommentBo.getValueId());
        marketComment.setType(wxCommentBo.getType());
        marketComment.setContent(wxCommentBo.getContent());
        marketComment.setUserId(user.getId());
        marketComment.setHasPicture(wxCommentBo.getHasPicture());
        marketComment.setPicUrls(wxCommentBo.getPicUrls());
        marketComment.setStar(wxCommentBo.getStar());
        marketComment.setAddTime(LocalDateTime.now());
        marketComment.setUpdateTime(LocalDateTime.now());
        int i = marketCommentMapper.insertSelective(marketComment);
        return i==1?marketComment:null;
    }
}
